import {
	baseUrl,
	baseUrlDemo,
	baseUrlTest
} from "@/config"

import {
	multipartUpload as upload,
	uploadTask as task //上传任务对象，同requestTask
} from "@/utils/xp-multipart/xp-multipart/xp-multipart.js"
/**
 *  封装request请求
 */
export default function request(
    url, 
	method = 'POST', 
	data = {},
	contentType = 'application/json', 
	needToken = true,
	needLoading = true) {
	return new Promise((resolve, reject) => {
		needLoading && uni.showLoading({
			title:'加载中',
			mask:true
		})
	
		

		let header = {
			'Content-Type': contentType,
		}
		const token = uni.getStorageSync('token') || ''
		if (token && needToken) {
			header.Authorization = 'Bearer ' + token
		}
		uni.request({
			url: baseUrl + url,
			data: data || '',
			header: header,
			method: method,
			success: (res) => {
				if (res.statusCode === 200) {
					if (res.data.code === 200) {
						resolve(res.data)
					} else if (res.data.code === 401) {
						uni.removeStorageSync('userInfo')
						uni.removeStorageSync('token')
						uni.removeStorageSync('systemEnv');
						uni.reLaunch({
							url: '/pages/login/login'
						})
						reject(res.data)
					} else if (res.data.code === 403) {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: function(res) {
								console.warn(res)
								uni.navigateBack()
							}
						})
					
						reject(res.data)
					} else {
						uni.showModal({
							title: '提示',
							content: res.data.msg || '服务器内部错误',
							showCancel: false,
							success: function(res) {}
						})
						reject(res.data)
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '服务器异常',
						showCancel: false,
						success: function(res) {}
					})
					reject(res.data);
				}
			},
			fail: (res) => {
				console.error("请求的地址======>", rootUrl + url)
				console.warn("***********************************************************");
				console.warn("返回失败===>", res)
				console.warn("返回的参数===>", JSON.stringify(res))
				console.warn("***********************************************************");
				reject(res);

				uni.showModal({
					title: '提示',
					content: '网络异常，请求超时',
					showCancel: false,
					success: function(res) {}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			},
		})
	})
}


export function uploadFile(url, filePath, name, key, fileData) {
	return new Promise((resolve, reject) => {
		const token = uni.getStorageSync('token') || ''
		let formData;
		if (key) {
			formData = {
				[key]: JSON.stringify(fileData)
			}
		}
		console.log(formData, key)
		uni.uploadFile({
			url: baseUrl + url,
			filePath: filePath,
			name: name,
			header: {
				Authorization: 'Bearer ' + token
			},
			formData: formData,

			success: (res) => {
				if (res.statusCode === 200) {
					if (res.data.code === 200) {
						resolve(res.data)
					} else if (res.data.code === 401) {
						uni.removeStorageSync('userInfo')
						uni.removeStorageSync('token')
						uni.removeStorageSync('systemEnv');
						uni.reLaunch({
							url: '/pages/login/login'
						})
						reject(res.data)
					} else if (res.data.code === 403) {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: function(res) {
								console.warn(res)
								uni.navigateBack()
							}
						})
						// uni.navigateTo({
						// 	url: '/pages/login/login'
						// })
						reject(res.data)
					} else {
						uni.showModal({
							title: '提示',
							content: res.data.msg || '服务器内部错误',
							showCancel: false,
							success: function(res) {}
						})
						reject(res.data)
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '服务器异常',
						showCancel: false,
						success: function(res) {}
					})
					reject(res.data);
				}
			},
			fail: (res) => {
				reject(res);

				uni.showModal({
					title: '提示',
					content: '网络异常，请求超时',
					showCancel: false,
					success: function(res) {}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			},
		});
	})
}

//多文件上传 微信小程序可以使用

export function uploadFiles(url, arr, key, fileData) {
	return new Promise((resolve, reject) => {
		const token = uni.getStorageSync('token') || ''

		let rootUrl = baseUrl
		let isArrey;
		// 是否是演示系统
		const systemEnv = uni.getStorageSync('systemEnv')
		if (systemEnv == 'test') {
			rootUrl = baseUrlTest
		} else if (systemEnv == 'demo') {
			rootUrl = baseUrlDemo
		}
		// let formData;
		// if(key){
		// 	formData = {
		// 			[key]:fileData
		// 	}
		// }else{
		// 	formData = fileData
		// }


		upload({
			url: rootUrl + url, //后端接口
			fields: {
				[key]: JSON.stringify(fileData)
			},
			header: {
				Authorization: 'Bearer ' + token,
			},
			files: arr,
			success: (res) => {
				console.error(res)
				console.warn("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
				console.warn("请求的地址======>", rootUrl + url)
				console.warn("请求的参数======>", res.data)
				console.warn("返回的参数===>", res.data)
				console.warn("返回的参数===>", JSON.stringify(res.data))
				console.warn("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");
				if (res.statusCode === 200) {
					if (res.data.code === 200) {
						resolve(res.data)
					} else if (res.data.code === 401) {
						uni.removeStorageSync('userInfo')
						uni.removeStorageSync('token')
						uni.removeStorageSync('systemEnv');
						uni.reLaunch({
							url: '/pages/login/login'
						})
						reject(res.data)
					} else if (res.data.code === 403) {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: function(res) {
								console.warn(res)
								uni.navigateBack()
							}
						})
						// uni.navigateTo({
						// 	url: '/pages/login/login'
						// })
						reject(res.data)
					} else {
						uni.showModal({
							title: '提示',
							content: res.data.msg || '服务器内部错误',
							showCancel: false,
							success: function(res) {}
						})
						reject(res.data)
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '服务器异常',
						showCancel: false,
						success: function(res) {}
					})
					reject(res.data);
				}
			},
			fail: (res) => {
				console.error("请求的地址======>", rootUrl + url)
				console.warn("***********************************************************");
				console.warn("返回失败===>", res)
				console.warn("返回的参数===>", JSON.stringify(res))
				console.warn("***********************************************************");
				reject(res);

				uni.showModal({
					title: '提示',
					content: '网络异常，请求超时',
					showCancel: false,
					success: function(res) {}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			},
		})

	})

}
